﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.IO;
using Aspose.Words;
using Aspose;
using Aspose.Words.Drawing;
using Aspose.Words.Reporting;
using Aspose.Words.Viewer;
using GPCSC.Web;
using System.Text;
using NickLee.Common;

namespace GPCSC.Web.sjs
{
    public partial class sqfk : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string ids = Usual.f_querystringToString("ggid");
                if (ids != "" && ids != null)
                {
                    //绑定最新的反馈表信息
                    f_bound(ids);
                }

                string iszuofei = IsZuoFei(ids);


                btnNew.OnClientClick = Window1.GetShowReference("GGHList.aspx?type=1&iszuofei="+iszuofei, "详情");
            }
        }

        private string IsZuoFei(string ids)
        {

            DataSet ds = new DataSet();
            Hashtable htable = new Hashtable();
            htable.Add("@ggid", ids);
            ds = new SqlReadXml().ReaderDataSet("sel_gglist_byid", htable);

            string iszuofei = string.Empty;
            if (ds.Tables[0].Rows.Count > 0)
            {
                iszuofei = Convert.ToString(ds.Tables[0].Rows[0]["IsZuoFei"]);
            }
            return iszuofei;
        }
        protected void Window1_Close(object sender, EventArgs e)
        {
            // f_bound();

        }
        protected void f_bound(string ids)
        {
            try
            {
                DataSet ds = new DataSet();
                Hashtable htable = new Hashtable();
                htable.Add("@SQGGID", ids);
                ds = new SqlReadXml().ReaderDataSet("sqtop", htable);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    UIFactory.UIFactory_Set(this, ds.Tables[0]);
                }
                else
                {
                    htable.Clear();
                    ds.Clear();
                    htable.Add("@ggid", ids);
                    ds = new SqlReadXml().ReaderDataSet("selggtablebyid", htable);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        MtxtD_SQ_GGSQ.Value = ds.Tables[0].Rows[0]["ggname"].ToString();
                    }
                }
                if (Usual.f_querystringToString("type") == "view")
                {
                    UIFactory.UIFactory_ReadOnly(this);
                    this.Button1.Visible = false;
                    this.Button2.Visible = false;
                }
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }

        //提交
        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                Hashtable htable = f_getElements();
                SQLServerDAL.databyhtable dtable = new SQLServerDAL.databyhtable();
                dtable.databyhtables(htable, "add", "SheQuFK", "", "");
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>locking();</script>");
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }
        public Hashtable f_getElements()
        {
            base.o_uiTable.Clear();
            base.o_uiTable = UIFactory.UIFactory_Get(this);
            byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
            Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
            Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
            base.o_uiTable.Add("SQLGID", users.CL_USER_SOID.ToString());
            base.o_uiTable.Add("tjdate", DateTime.Now);
            base.o_uiTable.Add("SQGGID", Usual.f_querystringToString("ggid"));
            base.o_uiTable.Add("postid", users.CL_POSTID_PK.ToString());
            return base.o_uiTable;
        }

        //下载
        protected void Button1_Click1(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                Hashtable htable = new Hashtable();
                htable = UIFactory.UIFactory_Get(this);
                byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
                Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
                Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
                htable.Add("name", users.USERNAME.ToString());
                htable.Add("dw", users.POSTName.ToString());
                htable.Add("date", DateTime.Now.ToString());
                DataTable tables = HashtableAndDatatable.Retable(htable);
                ds.Tables.Add(tables);
                cterWord1(ds);
            }
            catch
            {

            }
            finally
            {
                KillProcess("WINWORD");
            }
        }
        protected void cterWord1(DataSet ds)
        {
            try
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    Stream f = new FileStream(Server.MapPath("~") + @"\grid\sq.doc", FileMode.Open);
                    Document doc = new Document(f);
                    doc.MailMerge.Execute(ds.Tables[0]);
                    doc.MailMerge.DeleteFields();
                    doc.Save(Server.UrlEncode("进社区活动情况统计表.doc"), SaveFormat.Doc, Aspose.Words.SaveType.OpenInWord, Response);
                }
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }
        private void KillProcess(string processName)
        {
            System.Diagnostics.Process myproc = new System.Diagnostics.Process();
            //得到所有打开的进程 
            try
            {
                foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName))
                {
                    if (!thisproc.CloseMainWindow())
                    {
                        thisproc.Kill();
                    }
                }
            }
            catch (Exception Exc)
            {
                throw Exc;
            }
        }
    }
}